/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.bs.entity.BsDepartmentPatientEntity;
import io.renren.modules.bs.service.BsDepartmentPatientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 测试定时任务(演示Demo，可删除)
 *
 * testTask为spring bean的名称
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component("testTask")
public class TestTask implements ITask {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private BsDepartmentPatientService departmentPatientService;


	@Override
	public void run(String params){
		try {
			int i = Integer.parseInt(params);
			logger.debug("TestTask定时任务正在执行，参数为：{}", params);
			QueryWrapper<BsDepartmentPatientEntity> bsDepartmentPatientEntityQueryWrapper = new QueryWrapper<>();
			bsDepartmentPatientEntityQueryWrapper.eq("is_leave",1);
			bsDepartmentPatientEntityQueryWrapper.gt("DATE_SUB(CURDATE(), INTERVAL "+i+" DAY)","date(leave_hospital_date)");
			List<BsDepartmentPatientEntity> departmentPatientEntities = departmentPatientService.getListByDay(i);
			if(departmentPatientEntities.size() > 0){
				List<Long> idList = departmentPatientEntities.stream().map(BsDepartmentPatientEntity::getPatientId).collect(Collectors.toList());
				departmentPatientService.removeModelByIds(idList);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
}
